<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增角色 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '修改',
              auth: 'sysRole:edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '授权菜单',
              auth: 'sysRole:edit',
              onClick: handleGrantMenu.bind(null, record),
            },
            {
              label: '授权数据',
              auth: 'sysRole:edit',
              onClick: handleGrantData.bind(null, record),
            },
            {
              label: '删除',
              auth: 'sysRole:delete',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <RoleMenuDrawer @register="registerMenuDrawer" @success="handleSuccess" />
    <RoleDataDrawer @register="registerDataDrawer" />
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'SysRole',
});
</script>
<script lang="ts" setup>
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { getRoleListByPage, deleteRole } from '@/api/sys/system';
import { useDrawer } from '@/components/Drawer';
import RoleDrawer from './RoleDrawer.vue';
import RoleMenuDrawer from './RoleMenuDrawer.vue';
import RoleDataDrawer from './RoleDataDrawer.vue';
import { useMessage } from '@/hooks/web/useMessage';
import { columns, searchFormSchema } from './role.data';

const { createMessage } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerTable, { reload }] = useTable({
  title: '角色列表',
  api: getRoleListByPage,
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
  },
  useSearchForm: true,
  bordered: true,
  showIndexColumn: false,
  actionColumn: {
    width: 250,
    title: '操作',
    dataIndex: 'action',
    slots: { customRender: 'action' },
    fixed: 'right',
  },
});
const [registerMenuDrawer, { openDrawer: openMenuDrawer }] = useDrawer();
const [registerDataDrawer, { openDrawer: openDataDrawer }] = useDrawer();
function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}
function handleGrantMenu(record: Recordable) {
  openMenuDrawer(true, {
    record,
  });
}
function handleGrantData(record: Recordable) {
  openDataDrawer(true, {
    record,
  });
}

function handleDelete(record: Recordable) {
  console.log(record);
  deleteRole(record.id).then(() => {
    createMessage.success('删除成功');
    reload();
  });
}

function handleSuccess() {
  reload();
}
</script>
